package demo;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main2 {
	
	public static void main(String[] args) {
		
		int inputYear = 2021;
		
		List list = new ArrayList();
		//前台传来的年份是不是当前年份
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.FRIDAY);
		int year = calendar.getWeekYear();
		int thisWeek = 52;
		if (inputYear == year) {
			//如果是当前年份就计算已经过去的周次
			thisWeek = calendar.get(Calendar.WEEK_OF_YEAR);
		} else {
			year = inputYear;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
		String startYear = "";
		for (int i = 0; i < thisWeek; i++) {
			Calendar cal = Calendar.getInstance();

			// 设置每周的开始日期
			cal.setFirstDayOfWeek(Calendar.FRIDAY);

			cal.set(Calendar.YEAR, year);
			cal.set(Calendar.WEEK_OF_YEAR, (i+1));

			cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
			String beginDate = sdf.format(cal.getTime());
			String beginDateR = sdf2.format(cal.getTime());

			cal.add(Calendar.DAY_OF_WEEK, 6);
			String endDate = sdf.format(cal.getTime());
			String endDateR = sdf2.format(cal.getTime());

			Map m = new HashMap();
			m.put("value", i + 1);
			m.put("key", "第" + (i + 1) + "周 " + beginDate + "-" + endDate);
			m.put("beginDate", beginDateR);
			m.put("endDate", endDateR);
			if (i == 0) {//将本年起始周的第一天作为年累的begin参数
				startYear = beginDateR;
			}
			m.put("startYear", startYear);
			list.add(m);
		}
		
//		System.out.println(list.get(0));
		for (Object obj: list) {
			System.out.println(obj);
		}
	}

}
